Skip to main content

How to detect if a button has been pressed in a simple way

There are two ways to detect if a button has been pressed​


First way​

  • Just add a SUIKeyEventListener to the button in the interface in the properties panel, define its Key and then check it through a script.

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

@Override
public void start() {

}

@Override
public void repeat() {

if(Input.getKey("yourKey").isDown()) {

// your code

}

}

}

Second way​

  • Just create a public variable of type SUIButton and select your button on the interface in the properties panel.

⚠️ There are many ways to search for a component, to better understand this see the topic Search components, you can search your button in any of these ways. ⚠️

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

public SUIButton myButton;

@Override
public void start() {

}

@Override
public void repeat() {

if(myButton.isDown()) {

// your code

}

}

}

Understanding the code​

isDown()​

  • Detect touch when the button is touched.

isUp()​

  • Detects touch when the button is released.

isPressed()​

  • Detects touch while the button is being pressed.